package com.xxchuxing.driver.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.taxi.driver.common.AppConfig;
import com.taxi.driver.event.PayEvent;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        api = WXAPIFactory.createWXAPI(this, AppConfig.WX_APPID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {

    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0) {
                //支付成功
                EventBus.getDefault().post(new PayEvent(PayEvent.PAY_SUCCESS));
            } else {
                //支付失败
                EventBus.getDefault().post(new PayEvent(PayEvent.PAY_FAIL));
                EventBus.getDefault().post(new PayEvent(PayEvent.PAY_REMOVE));
            }
        }
        finish();
    }
}